$(function () {
    updateMyPic();
    updateMyPwdCheck();
    selMyInfo();
});


//个人信息nav切换
function topNavClick(op) {
    var c = $(op).attr("class");
    if (c == "top-nav") {
        $("#top").children("div").attr("class", "top-nav");
        $(op).attr("class", "top-nav-now");
    }
    var text = $(op).text();
    if (text == "我的资料") {
        $("#middle-mydata").css("display", "block");
        $("#middle-mypic").css("display", "none");
        $("#middle-mypwd").css("display", "none");
        $("#middle-myaccount").css("display", "none");
    } else if (text == "头像") {
        $("#middle-mydata").css("display", "none");
        $("#middle-mypic").css("display", "block");
        $("#middle-mypwd").css("display", "none");
        $("#middle-myaccount").css("display", "none");
        $("#show-img").css("background-image", "url(\""+ctx+"/"+user_pic+"\")");
        $("#radio-img").css("background-image", "url(\""+ctx+"/"+user_pic+"\")");
    } else if (text == "密码") {
        $("#middle-mydata").css("display", "none");
        $("#middle-mypic").css("display", "none");
        $("#middle-mypwd").css("display", "block");
        $("#middle-myaccount").css("display", "none");
    } else if (text == "账号绑定") {
        $("#middle-mydata").css("display", "none");
        $("#middle-mypic").css("display", "none");
        $("#middle-mypwd").css("display", "none");
        $("#middle-myaccount").css("display", "block");
    }
}


//我的资料信息加载
function selMyInfo() {
    var phone = $("#text-phone").val();
    if (phone == null || phone == ""){
        $("#phone").children("data-msg").text("");
        $("#phone").children("data-msg2").text("手机号未绑定");
    }else{
        $("#phone").children("data-msg").text("您已完成手机号绑定，已正式成为社区实名用户。");
        $("#phone").children("data-msg2").text("手机号暂不支持修改。");
    }
    var email = $("#text-email").val();
    if (email == null || email == ""){
        $("#email").children("data-msg").text("");
        $("#email").children("data-msg2").text("邮箱未激活");
    }else{
        $("#email").children("data-msg").text("您的邮箱已激活，可通过邮箱找回密码。");
        $("#email").children("data-msg2").text("邮箱暂不支持修改。");
    }
    if(sex == "男"){
        $("#male").prop("checked","true");
    }else if(sex == "女"){
        $("#female").prop("checked","true");
    }
    $("#text-department").val(""+department+"");
    $("#text-profession").val(""+profession+"");
    $("#text-classes").val(""+classes+"");
}


//修改我的资料
function updateMyInfo() {
    var phone = $("#text-phone").val();
    var email = $("#text-email").val();
    var nickname = $("#text-nickname").val();
    var checked = $("#male").prop("checked");
    var sex = "";
    if(checked == true){
        sex = "男";
    }else if(checked == false){
        sex = "女";
    }
    var department = $("#text-department").val();
    var profession = $("#text-profession").val();
    var classes = $("#text-classes").val();
    var realname = $("#text-realname").val();
    $.ajax({
        type: 'post',
        url: ctx + "/updateMyInfo",   //提交到的url
        cache: false,
        data: {"phone": phone, "email": email, "nickname": nickname, "sex": sex, "department": department, "profession": profession, "classes": classes, "realname": realname},    //提交的参数
        dataType: 'json',
        success: function (data) {
            if (data.message == 1){
                alert("修改成功");
            }else{
                alert("修改失败");
            }
            parent.location.reload(true);
        },
        error: function (data) {
            alert("查询失败！");
        }
    });
}


//上传头像
function updateMyPic() {
    layui.use('upload', function () {
        var $ = layui.jquery
            , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            , url: ctx + "/updateMyPic"
            , before: function (obj) {
                if (obj.message != 0) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        // $('#demo1').attr('src', result); //图片链接（base64）
                        $("#show-img").css("background-image", "url("+result+")");
                        $("#radio-img").css("background-image", "url("+result+")");
                        parent.$("#mypic").css("background-image", "url("+result+")");
                        return layer.msg('上传成功');
                    });
                }
            }
            , done: function (res) {
                //如果上传失败
                if (res.message == "0") {
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            , error: function (data) {
                if (data.message == "0") {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            }
        });

    });
}


//修改密码数据校验
function updateMyPwdCheck() {
    $("#text-old-pwd").blur(function () {
        var oldpwd = $("#text-old-pwd").val();
        var newpwd = $("#text-new-pwd").val();
        if (oldpwd == null || oldpwd == "") {
            $("#old-pwd").children(".data-msg3").text("旧密码不能为空");
            $("#btn-update-pwd").css("cursor","not-allowed");
        } else {
            if (newpwd == oldpwd) {
                $("#new-pwd").children(".data-msg3").text("新密码必须与旧密码不一致");
                $("#btn-update-pwd").css("cursor","not-allowed");
            }
        }
    });
    $("#text-new-pwd").blur(function () {
        var oldpwd = $("#text-old-pwd").val();
        var newpwd = $("#text-new-pwd").val();
        var confirmpwd = $("#text-confirm-pwd").val();
        if (newpwd == null || newpwd == "") {
            $("#new-pwd").children(".data-msg3").text("新密码不能为空");
            $("#btn-update-pwd").css("cursor","not-allowed");
        } else {
            if (newpwd.length < 6 || newpwd.length > 16) {
                $("#new-pwd").children(".data-msg3").text("新密码长度必须是6-16位");
                $("#btn-update-pwd").css("cursor","not-allowed");
            } else {
                if (newpwd == oldpwd) {
                    $("#new-pwd").children(".data-msg3").text("新密码必须与旧密码不一致");
                    $("#btn-update-pwd").css("cursor","not-allowed");
                }else {
                    if (newpwd != confirmpwd) {
                        $("#confirm-pwd").children(".data-msg3").text("两次输入的密码必须一致");
                        $("#btn-update-pwd").css("cursor","not-allowed");
                    }
                }
            }
        }
    });
    $("#text-confirm-pwd").blur(function () {
        var newpwd = $("#text-new-pwd").val();
        var confirmpwd = $("#text-confirm-pwd").val();
        if (newpwd != confirmpwd) {
            $("#confirm-pwd").children(".data-msg3").text("两次输入的密码必须一致");
            $("#btn-update-pwd").css("cursor","not-allowed");
        }
    });
}
//修改密码数据校验2
function hideMsg(op) {
    $(op).next().text("");
    var oldpwd = $("#text-old-pwd").val();
    var newpwd = $("#text-new-pwd").val();
    var confirmpwd = $("#text-confirm-pwd").val();
    var t1 = $("#old-pwd").children(".data-msg3").text().length;
    var t2 = $("#new-pwd").children(".data-msg3").text().length;
    var t3 = $("#confirm-pwd").children(".data-msg3").text().length;
    if(oldpwd != newpwd && t2 == "新密码必须与旧密码不一致"){
        $("#new-pwd").children(".data-msg3").text("");
    }
    if(newpwd == confirmpwd && t3 == "两次输入的密码必须一致"){
        $("#confirm-pwd").children(".data-msg3").text("");
    }
    if(t1+t2+t3 == 0){
        $("#btn-update-pwd").css("cursor","pointer");
    }
}


//修改密码
function updateMyPwd() {
    var oldpwd = $("#text-old-pwd").val();
    var newpwd = $("#text-new-pwd").val();
    var c = $("#btn-update-pwd").css("cursor");
    if(c == "pointer"){
        $.ajax({
            type: 'post',
            url: ctx + "/updateMyPwd",   //提交到的url
            cache: false,
            data: {"oldpwd": oldpwd, "newpwd": newpwd},    //提交的参数
            dataType: 'json',
            success: function (data) {
                if (data.message == 1){
                    alert("修改成功");
                }else{
                    alert("修改失败");
                }
                parent.location.reload(true);
            },
            error: function (data) {
                alert("查询失败！");
            }
        });
    }
}